package xFGE.Loader
{
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.*;
	import flash.system.ApplicationDomain;
	internal class SWFLoader extends LoaderBase
	{
		//loader
		private var _loader:Loader;
		
		public function SWFLoader()
		{
			super(xLoader.LOADER_TYPE_SWF);
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.OPEN,super.onStart);
			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,super.onProgress);
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
			_loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS,super.onHttpStatus);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,super.onIOError);
			
		}
		
		/**
		 * 载入 
		 * @param resourceObj
		 * 
		 */
		override public function Load(resourceObj:ResourceObj):void
		{
			super.Load(resourceObj);
			try{
				_loader.load(resourceObj.url);
			}catch(e:SecurityError){
				trace(e);
			}
		}
		
		/**
		 * 载入完成 
		 * @param eveObj
		 * 
		 */
		override protected function onComplete(eveObj:Event):void
		{
			var loaderInfo:LoaderInfo = eveObj.target as LoaderInfo;
			var domain:ApplicationDomain = loaderInfo.applicationDomain;
			// 载入带有绑定类的swf，_content保存的是该类的类对象，使用时直接new既可
			if(domain.hasDefinition(_resourceObj.swfClassName)){
				var ClassName:Class = domain.getDefinition(_resourceObj.swfClassName) as Class;
				_content = ClassName;
			}else{//载入无绑定类的swf
				_content = loaderInfo.content;
			}
			super.onComplete(eveObj);
		}
		
		override public function Clear():void
		{
			_loader.contentLoaderInfo.removeEventListener(Event.OPEN,super.onStart);
			_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,super.onProgress);
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onComplete);
			_loader.contentLoaderInfo.removeEventListener(HTTPStatusEvent.HTTP_STATUS,super.onHttpStatus);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,super.onIOError);
		}
	}
}